#
#   MenuSystem.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Salvatore S. Gionfriddo
#   Contributors: Nicholas F. Hoover
#   Created: 2007.07.24
#   Last Modified: 2007.07.24
#

import hud, pygame, Messages, EventHandler
from Options import Options

class Menu:
    (OPTION_NAME, OPTION_FUNCTION, OPTION_CHOICES, OPTION_SELECTION) = range(4)
    def __init__(self, game, screen, options, splash = None):
        self.options = options
        self.game = game
        self.screen = screen
        self.x = 300
        self.y = 200
        self.continue_display = True
        self.splash = splash
    def display(self):
        menuSelection = 0
        MENU_SIZE = len(self.options)
        self.continue_display = True
        while self.continue_display:
            if self.splash:
                self.screen.blit(self.splash,(0,0))
            else:
                self.game.background.draw()
            self.game.tintScreen()

            labels = []
            i = 0
            for option in self.options:
                text = option[self.OPTION_NAME]
                if option[self.OPTION_CHOICES]:
                    text += ": "
                    index = option[self.OPTION_SELECTION]
                    text += option[self.OPTION_CHOICES][index]
                labels.append(hud.label(self.screen,(self.x,self.y+(i*50)),text))
                i += 1
            
            for label in labels:
                label.setSize(20)
                label.setBackColor((255,255,255))

            for i in range(MENU_SIZE):
                if i == menuSelection:
                    labels[i].setTextColor((0,255,0))
                    labels[i].setAlpha(255)
                else:
                    labels[i].setTextColor((0,0,0))
                    labels[i].setAlpha(200)
                labels[i].draw()
            
            #draw instructions
            mi_text = ["Press Up or Down to select menu items.",
                       "Press Left or Right to change the option.",
                       "Press Enter to select the item."]
            menu_instructions = hud.text_box(self.screen, (self.x-50, self.y-100), mi_text)
            menu_instructions.setAlpha(240)
            menu_instructions.draw()
            
            pygame.display.flip()
            event = pygame.event.wait()
            if event.type == pygame.QUIT:
                quit()
            
            elif event.type == pygame.KEYDOWN:
                key = event.key
                if key == pygame.K_ESCAPE: # EXIT GAME
                    quit()
                elif key == pygame.K_DOWN: # SELECT NEXT ITEM
                    menuSelection += 1
                    if menuSelection >= MENU_SIZE: menuSelection = 0
                elif key == pygame.K_UP: # SELECT PREVIOUS ITEM
                    menuSelection -=1
                    if menuSelection < 0: menuSelection = MENU_SIZE -1
                elif key == pygame.K_LEFT: # CHANGE OPTION
                    if self.options[menuSelection][self.OPTION_CHOICES]:
                        self.options[menuSelection][self.OPTION_SELECTION] -= 1
                        if self.options[menuSelection][self.OPTION_SELECTION] < 0:
                            self.options[menuSelection][self.OPTION_SELECTION] = 0
                elif key == pygame.K_RIGHT: # CHANGE OPTION
                    if self.options[menuSelection][self.OPTION_CHOICES]:
                        self.options[menuSelection][self.OPTION_SELECTION] += 1
                        if self.options[menuSelection][self.OPTION_SELECTION] >= len(self.options[menuSelection][self.OPTION_CHOICES]):
                            self.options[menuSelection][self.OPTION_SELECTION] = len(self.options[menuSelection][self.OPTION_CHOICES]) - 1
                elif key == pygame.K_RETURN: # SELECT ITEM
                    if self.options[menuSelection][self.OPTION_FUNCTION]:
                        self.options[menuSelection][self.OPTION_FUNCTION]()
class MenuSystem:
    (MAIN, OPTIONS) = range(2)
    def __init__(self, game, screen, gameOptions, splash = None, setMission = None, highScores = {}):
        self.menus = range(2)
        self.setMission = setMission
        self.game = game
        self.screen = screen
        self.current_menu = self.MAIN
        missions = ["INTRO", "BEGINNER","CHALLENGE","EXPERT"]
        scores = []
        self.gameOptions = gameOptions
        for mission in missions:
            scores.append(mission + " - " + str(highScores[mission]))
        
        main_menu_options = [["PLAY", self.play, None, 0],
                   ["MISSION", self.play, missions, 0],
                   ["OPTIONS", (lambda: self.setMenu(self.OPTIONS)), None, 0],
                   ["HIGH SCORES", None, scores, 0],
                   ["INSTRUCTIONS", self.drawInstructions, None, 0],
                   ["QUIT", self.quit, None, 0]]

        if self.gameOptions.VIDEO['FULLSCREEN']: fullscreen_status = 0
        else: fullscreen_status = 1
        if self.gameOptions.SOUND['MUSIC_ON']:music_status = 0
        else: music_status = 1
        options_menu_options = [["Fullscreen", self.setFullscreen, ["On", "Off"], fullscreen_status],
                                ["Music", self.setMusic, ["On", "Off"],0,],
                                ["SAVE", self.saveOptions, None, 0],
                                ["BACK", self.back, None, 0]]
        self.menus[self.MAIN] = Menu(game, screen, main_menu_options, splash)
        self.menus[self.OPTIONS] = Menu(game, screen, options_menu_options, splash)
    def display(self):
        self.menus[self.current_menu].display()
    def back(self):
        self.play()
        self.setMenu(self.MAIN)
    def play(self):
        if self.setMission:
            #SMYMADHAZZZZZZZZZZZZXXXXXXXXXXX
            self.setMission(self.menus[self.MAIN].options[1][2][self.menus[self.MAIN].options[1][3]])
        self.menus[self.current_menu].continue_display = False
    def setMenu(self, menu):
        self.current_menu = menu
        self.display()
    def setFullscreen(self):
        if self.menus[self.OPTIONS].options[0][self.menus[self.OPTIONS].OPTION_SELECTION] == 0:
            self.gameOptions.setFullscreen(True)
        elif self.menus[self.OPTIONS].options[0][self.menus[self.OPTIONS].OPTION_SELECTION] == 1:
            self.gameOptions.setFullscreen(False)
    def setMusic(self):
        if self.menus[self.OPTIONS].options[1][self.menus[self.OPTIONS].OPTION_SELECTION] == 0:
            self.gameOptions.setMusicOn(True)
        elif self.menus[self.OPTIONS].options[1][self.menus[self.OPTIONS].OPTION_SELECTION] == 1:
            self.gameOptions.setMusicOn(False)
    def saveOptions(self):
        self.setFullscreen()
        self.setMusic()
        self.gameOptions.writeOptionsFile()
    def back(self):
        self.play()
        self.setMenu(self.MAIN)
    def drawInstructions(self):
        self.game.tintScreen()
        Messages.printInstructions(self.screen)
        pygame.display.flip()
        EventHandler.waitForKeyOrQuit(Options.CONTROLS['ENTER'])
    def quit(self):
        quit()
